<template>
  <div class="flex flex-col h-full">
    <a-card>
      <div class="flex justify-between">
        <a-form ref="formRef" layout="inline" class="flex-1" :model="formState">
          <a-form-item label="日期范围" name="rangePicker">
            <a-rangePicker v-model:value="formState['rangePicker']" format="YYYY-MM-DD" value-format="YYYY-MM-DD" @change="getTollCollectorData" />
          </a-form-item>
          <a-form-item label="收费员" name="czy" class="w-72">
            <a-select
              v-model:value="formState.czy"
              mode="multiple"
              :max-tag-count="2"
              placeholder="请选择"
              :options="wardData"
              :disabled="!isDeptPermissions && !isAllPermissions"
              @change="onSelectChange"
            />
          </a-form-item>
        </a-form>
        <a-space>
          <a-button @click="onReset">重置</a-button>
          <a-button type="primary" @click="getData">搜索</a-button>
          <!-- <a-button type="primary" ghost @click="onPrint">打印</a-button> -->
          <a-button type="primary" ghost @click="chargeOnPrint">收费报表</a-button>
          <a-button v-if="isMSG" type="primary" ghost @click="registerOnPrint">挂号报表</a-button>
        </a-space>
      </div>
    </a-card>
    <div class="flex justify-center overflow-y-scroll">
      <a-card class="mt-2 w-[1200px] justify-center" :body-style="{ height: '100%', overflow: 'auto' }" :loading="loading">
        <BaseTitle title="统计范围" />
        <BaseTable max-fill :data-source="dataSource?.checkoutTotleResponseList" :columns="columns" />
        <div class="flex justify-between mt-2">
          <BaseTitle title="收款汇总" />
          <div class="w-40">
            <!-- <span v-if="chargeRole"> &nbsp; 收费员: {{ chargeRole }} </span> -->
          </div>
        </div>
        <a-descriptions class="descriptions" bordered :column="5" :label-style="{ width: '135px' }" :content-style="{ width: '100px' }">
          <a-descriptions-item label="收费小计">{{ formatAmount(dataSource.pjxxsrResponse?.hjskhz || 0) }}</a-descriptions-item>
          <a-descriptions-item label="预收款支付">{{ formatAmount(dataSource.pjxxsrResponse?.hjskyskzf || 0) }}</a-descriptions-item>
          <a-descriptions-item label="优惠小计">{{ formatAmount(dataSource.pjxxsrResponse?.hjskyhhj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="分币误差">{{ formatAmount(dataSource.pjxxsrResponse?.hjskfbhj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="收款合计">{{ formatAmount(dataSource.pjxxsrResponse?.hjskhj || 0) }}</a-descriptions-item>
          <a-descriptions-item v-for="item in dataSource.hjskResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
          <a-descriptions-item label="医保">{{ formatAmount(dataSource.pjxxsrResponse?.ybzfhj || 0) }}</a-descriptions-item>
        </a-descriptions>

        <BaseTitle title="预收款收入" class="mt-2" />
        <a-descriptions class="descriptions" bordered :column="5" :label-style="{ width: '135px' }" :content-style="{ width: '100px' }">
          <a-descriptions-item label="收费小计">{{ formatAmount(dataSource.pjxxsrResponse?.yjskhz || 0) }}</a-descriptions-item>
          <a-descriptions-item label="(其中退费小计)">{{ formatAmount(dataSource.pjxxsrResponse?.yjsktfhj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="优惠小计">{{ formatAmount(dataSource.pjxxsrResponse?.yjskybzfhj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="分币误差">{{ formatAmount(dataSource.pjxxsrResponse?.yjskfbwc || 0) }}</a-descriptions-item>
          <!-- <a-descriptions-item label="退费小计">{{ formatAmount(dataSource.pjxxsrResponse?.yjsktfhj || 0) }}</a-descriptions-item> -->
          <a-descriptions-item label="预收款合计">{{ formatAmount(dataSource.pjxxsrResponse?.yjskhj || 0) }}</a-descriptions-item>
          <!-- <a-descriptions-item label="预收款合计">{{ formatAmount(dataSource.pjxxsrResponse?.sjsr || 0) }}</a-descriptions-item> -->
          <a-descriptions-item v-for="item in dataSource.yjsrResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
          <a-descriptions-item label="医保">-</a-descriptions-item>
        </a-descriptions>

        <BaseTitle title="会员收入" class="mt-2" />
        <a-descriptions class="descriptions" bordered :column="3" :label-style="{ width: '135px' }" :content-style="{ width: '100px' }">
          <a-descriptions-item label="收费小计">{{ formatAmount(dataSource.pjxxsrResponse?.hyskxj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="(其中赠送小计)">{{ formatAmount(dataSource.pjxxsrResponse?.hyskzsje || 0) }}</a-descriptions-item>
          <a-descriptions-item label="收款合计">{{ formatAmount(dataSource.pjxxsrResponse?.hyskhj || 0) }}</a-descriptions-item>
          <a-descriptions-item v-for="item in dataSource.hysrResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
        </a-descriptions>

        <BaseTitle title="营业收入" class="mt-2" />
        <a-descriptions class="descriptions" bordered :column="5" :label-style="{ width: '135px' }" :content-style="{ width: '100px' }">
          <a-descriptions-item label="收费小计">{{ formatAmount(dataSource.pjxxsrResponse?.yysrzfhz || 0) }}</a-descriptions-item>
          <a-descriptions-item label="(其中退费小计)">{{ formatAmount(dataSource.pjxxsrResponse?.yysrtfhj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="优惠小计">{{ formatAmount(dataSource.pjxxsrResponse?.yysryhhj || 0) }}</a-descriptions-item>
          <a-descriptions-item label="分币误差">{{ formatAmount(dataSource.pjxxsrResponse?.yysrfbwc || 0) }}</a-descriptions-item>
          <a-descriptions-item label="收款合计">{{ formatAmount(dataSource.pjxxsrResponse?.zfzfhj || 0) }}</a-descriptions-item>
          <a-descriptions-item v-for="item in dataSource.yysrResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
          <a-descriptions-item label="医保">{{ formatAmount(dataSource.pjxxsrResponse?.ybzfhj || 0) }}</a-descriptions-item>
        </a-descriptions>

        <BaseTitle title="营业收入（项目类别)" class="mt-2" />
        <a-descriptions
          v-if="dataSource.sflbResponse && dataSource.sflbResponse.length > 0"
          class="descriptions"
          bordered
          :column="5"
          :label-style="{ width: '135px' }"
          :content-style="{ width: '100px' }"
        >
          <a-descriptions-item v-for="item in dataSource.sflbResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
        </a-descriptions>
        <!-- <a-descriptions class="descriptions" bordered :column="4">
      <a-descriptions-item>汇总</a-descriptions-item>
      <a-descriptions-item>{{ formatAmount(dataSource.pjxxsrResponse?.yjskhz || 0) }}</a-descriptions-item>
      <a-descriptions-item label="退费合计">{{ formatAmount(dataSource.pjxxsrResponse?.yjsktfhj || 0) }}</a-descriptions-item>
      <a-descriptions-item label="合计">{{ formatAmount(dataSource.pjxxsrResponse?.yjskhj || 0) }}</a-descriptions-item>
      <a-descriptions-item v-for="item in dataSource.yjsrResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
    </a-descriptions>
    <div v-if="dataSource.sflbResponse && dataSource.sflbResponse.length > 0" class="text-center py-2 font-bold">项目类别</div>
    <a-descriptions v-if="dataSource.sflbResponse && dataSource.sflbResponse.length > 0" class="descriptions" bordered :column="4">
      <a-descriptions-item v-for="item in dataSource.sflbResponse" :key="item.bh" :label="item.mc">{{ formatAmount(item.sjje) }}</a-descriptions-item>
    </a-descriptions> -->
        <BaseTitle title="票据信息" class="mt-2" />
        <a-descriptions class="descriptions" bordered :column="3" :label-style="{ width: '135px' }" :content-style="{ width: '100px' }">
          <a-descriptions-item label="开始发票号">{{ dataSource.pjxxsrResponse?.ksfph || '-' }}</a-descriptions-item>
          <a-descriptions-item label="结束发票号">{{ dataSource.pjxxsrResponse?.jsfph || '-' }}</a-descriptions-item>
          <a-descriptions-item label="作废发票号">{{ dataSource.pjxxsrResponse?.zffph || '-' }}</a-descriptions-item>
        </a-descriptions>
        <div class="border p-4 border-t-0 border-black -mt-[2px]">
          收费退费共：{{ dataSource.pjxxsrResponse?.sfcs || 0 }}次，其中退费：{{ dataSource.pjxxsrResponse?.tfcs || 0 }}次
        </div>
      </a-card>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import dayjs from 'dayjs'
import { message } from 'ant-design-vue'
import { formatAmount } from '@/utils/format'
import { post } from '@/utils/request'
import { getSystemConfig } from '@/utils/systemConfig'
import useUserStore from '@/store/user'

const { empId } = useUserStore()
const formRef = ref()
const wardData = ref([])
const formState = ref({
  rangePicker: [dayjs().format('YYYY-MM-DD'), dayjs().format('YYYY-MM-DD')],
  czy: []
})
const chargeRole = ref('')
const roleArr = ref([])
const isMSG = ref(false)
const columns = ref([
  {
    title: '序号',
    dataIndex: 'xh',
    width: 80
  },
  {
    title: '结账周期',
    dataIndex: 'jzsj'
  },
  {
    title: '操作人',
    dataIndex: 'empName'
  },
  {
    title: '操作人编号',
    dataIndex: 'empId'
  }
])
const onSelectChange = (val) => {
  roleArr.value = []
  wardData.value.forEach((i) => {
    val.forEach((j) => {
      if (i.value === j) {
        roleArr.value.push(i.label)
      }
    })
  })
  chargeRole.value = roleArr.value.join(',')
  getData()
}
const onReset = () => {
  formRef.value.resetFields()
  chargeRole.value = ''
}
const params = computed(() => {
  return {
    czy: formState.value.czy,
    stdate: formState.value['rangePicker']?.[0],
    eddate: formState.value['rangePicker']?.[1]
  }
})
const { loading, dataSource, getData } = useFetch({
  url: '/outp/oupCharge/getChargeDailyReport',
  params,
  immediate: false
})

// 收费员
const getTollCollectorData = async () => {
  const res = await post('/outp/mzdj/getTollCollectors', {
    stdate: formState.value['rangePicker']?.[0],
    eddate: formState.value['rangePicker']?.[1]
  })
  wardData.value = res.data.map((item) => {
    return {
      label: item.empName,
      value: item.empId
    }
  })
  if (isAllPermissions.value) {
    formState.value.czy = []
  } else {
    if (isDeptPermissions.value) {
      if (wardData.value.length > 0) {
        formState.value.czy = []
        wardData.value.forEach((item) => {
          if (item) formState.value.czy.push(item.value)
        })
      } else {
        formState.value.czy = []
      }
    } else {
      formState.value.czy = [empId]
    }
  }
  // if (!isDeptPermissions.value && !isAllPermissions.value && wardData.value.length === 1) {
  //   formState.value.czy = [wardData.value[0]?.value]
  // }
  getData()
}
const isDeptPermissions = ref(false)
const isAllPermissions = ref(false)
// 获取数据权限
const getSystemList = async () => {
  const { code, data } = await post('/operation/opex/getEmpSystemDataPermissionsList', { empId })
  if (code !== 0) return message.error('获取数据权限失败')
  isDeptPermissions.value = data.some((i) => i.permissonCode === 'system:data:view_dept_charge_statistics')
  isAllPermissions.value = data.some((i) => i.permissonCode === 'system:data:view_all_charge_statistics')
  getTollCollectorData()
}
// const onPrint = async () => {
//   const params = {
//     stdate: formState.value['rangePicker']?.[0],
//     eddate: formState.value['rangePicker']?.[1],
//     czy: formState.value.czy
//   }
//   // window.open(`/api/v1/outp/outPrint/printDailyChargeStatements?${new URLSearchParams(params)}`)
//   const { code, msg, data } = await post('/outp/outPrint/getDailyChargeStatements', params)
//   if (code !== 0) return message.error(msg)
//   window.openExe?.rpt(data) //调用winform打印
// }
// 收费报表
const chargeOnPrint = async () => {
  const params = {
    stdate: formState.value['rangePicker']?.[0],
    eddate: formState.value['rangePicker']?.[1],
    czy: formState.value.czy
  }
  if (isMSG.value) {
    const { code, msg, data } = await post('/outp/outPrint/getOutpDailyOutpatientPaymentReport', params)
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
    const res = await post('/outp/outPrint/getOutpatientEmergencyFeePaymentDailyPrint', params)
    if (res.code !== 0) return message.error(res.msg)
    window.openExe?.rpt(res.data) //调用winform打印
  } else {
    const { code, msg, data } = await post('/outp/outPrint/DailyReportPrinting', params)
    if (code !== 0) return message.error(msg)
    window.openExe?.rpt(data) //调用winform打印
  }
}
// 挂号报表
const registerOnPrint = async () => {
  const params = {
    stdate: formState.value['rangePicker']?.[0],
    eddate: formState.value['rangePicker']?.[1],
    czy: formState.value.czy
  }
  const { code, msg, data } = await post('/outp/outPrint/getOutpRegPaymentDailyReportPrint', params)
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}

onMounted(() => {
  getSystemList()
  isMSG.value = getSystemConfig('OUTP_CHARGE_DAY_PRINT_MODE') === '2' //门诊收费每日报表打印模式  0：通用|1：上海黑马|2：茗视光
})
</script>
<style lang="less" scoped>
.descriptions {
  :deep(.ant-descriptions-view) {
    overflow: hidden;
    border-top: 1px solid #222222;
    border-left: 1px solid #222222;
    .ant-descriptions-row {
      text-align: right;
      border-bottom: 1px solid #222222;
      .ant-descriptions-item-label {
        padding: 16px 12px;
        text-align: right;
        border-inline-end: 1px solid #222222;
      }
      .ant-descriptions-item-content {
        border-inline-end: 1px solid #222222;
        padding: 16px 12px;
      }
    }
  }
}
</style>
